using System;
using System.Text;
using System.ComponentModel;
using CodeSmith.Engine;
using System.Data;
using System.Diagnostics;
using SchemaExplorer;

namespace CodeSmith.BaseTemplates
{
	public class SqlCodeTemplate : CodeTemplate
	{

		protected static Type ConvertType(DbType dbType)
		{
			Type toReturn = typeof(DBNull);
			switch (dbType)
			{
				case DbType.UInt64:
					toReturn = typeof(UInt64);
					break;
		
				case DbType.Int64:
					toReturn = typeof(Int64);
					break;
		
				case DbType.Int32:
					toReturn = typeof(Int32);
					break;
		
				case DbType.UInt32:
					toReturn = typeof(UInt32);
					break;
		
				case DbType.Single:
					toReturn = typeof(float);
					break;
		
				case DbType.Date:
				case DbType.DateTime:
				case DbType.Time:
					toReturn = typeof(DateTime);
					break;
		
				case DbType.String:
				case DbType.StringFixedLength:
				case DbType.AnsiString:
				case DbType.AnsiStringFixedLength:
					toReturn = typeof(string);
					break;
		
				case DbType.UInt16:
					toReturn = typeof(UInt16);
					break;
		
				case DbType.Int16:
					toReturn = typeof(Int16);
					break;
		
				case DbType.SByte:
					toReturn = typeof(byte);
					break;
		
				case DbType.Object:
					toReturn = typeof(object);
					break;
		
				case DbType.VarNumeric:
				case DbType.Decimal:
					toReturn = typeof(decimal);
					break;
		
				case DbType.Currency:
					toReturn = typeof(double);
					break;
		
				case DbType.Binary:
					toReturn = typeof(byte[]);
					break;
		
				case DbType.Double:
					toReturn = typeof(Double);
					break;
		
				case DbType.Guid:
					toReturn = typeof(Guid);
					break;
		
				case DbType.Boolean:
					toReturn = typeof(bool);
					break;
			}
		
			return toReturn;
		}

		public string GetAppName(bool isCore, TableSchema tableSchema) {
			string appName = "";
			if (isCore){
				appName = "Core";
			}
			else {
				appName = tableSchema.Database.Name.Substring(6);
			}
			return appName;
		}
		
		public string GetModelNamespace(bool isCore, string appName)
		{
			string res = "";
			if (isCore) {
				res = "EzDesk.Core.Model";
			} else {
				res = string.Format("EzDesk.Apps.Model.{0}", appName);
			}
			return res;
		}
		
		public string GetClassName(TableSchema table, string prefix) {
			return table.Name.Substring(prefix.Length);
		}
		
		public string GetRepositoryName(TableSchema table, string prefix) {
			return GetClassName(table, prefix) + "Repository";
		}
		
		public string GetRepositoryInterfaceName(TableSchema table, string prefix) {
			return "I" + GetClassName(table, prefix) + "Repository";
		}
		
		
		public string GetCamelCaseName(string value)
		{
			return value.Substring(0, 1).ToLower() + value.Substring(1);
		}
		
		
		public string GetMemberVariableName(string value)
		{
			string memberVariableName = "_" + GetCamelCaseName(value);
			
			return memberVariableName;
		}
		
		
		
	}
}
